﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace TestMinAndMaxWinForm
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 当前窗体状态是否为最小化
        /// </summary>
        private static bool IsFormMinSize = false;
        protected override void WndProc(ref Message m)
        {
            const int WM_SYSCOMMAND = 0x112;
            const int SC_CLOSE = 0xf060;//关闭  
            const int SC_MINSIZE = 0xf020;//最大化  
            const int SC_MAXISIZE = 0xf030;//最小化  
            const int SC_NORMAL = 0xf120;//还原  
            const int SC_DOUBLECLICK = 0xf122;//双击窗体标题栏  
            if ((m.Msg == WM_SYSCOMMAND) && ((int)m.WParam == SC_CLOSE))//关闭  
            {
               
                MessageBox.Show("关闭");
               // return;
            }
            if ((m.Msg == WM_SYSCOMMAND) && ((int)m.WParam == SC_MAXISIZE))//最大化  
            {
                IsFormMinSize = false;
                this.WindowState = FormWindowState.Maximized;
               //MessageBox.Show("最大化");
                this.Show();
                return;
            }
            if ((m.Msg == WM_SYSCOMMAND) && ((int)m.WParam == SC_MINSIZE))//最小化  
            {
                if (IsFormMinSize == false)
                {
                    LocatePosition();
                    IsFormMinSize = true;
                }
                else
                {
                    this.WindowState = FormWindowState.Maximized;
                    IsFormMinSize = false;
                }
                
               // MessageBox.Show("最小化");
                //this.Hide();
                return;
            }
            if ((m.Msg == WM_SYSCOMMAND) && ((int)m.WParam == SC_NORMAL))//还原  
            {
                // this.WindowState = FormWindowState.Maximized;
                MessageBox.Show("还原");
                //this.Show();
                return;
            }
            if ((m.Msg == WM_SYSCOMMAND) && ((int)m.WParam == SC_DOUBLECLICK))///双击窗体标题栏  
            {
                MessageBox.Show("双击窗体标题栏 ");
                return;
            }
            //if (m.Msg == WM_SYSCOMMAND)//用来获取用户触发事件的16进制参数，通过科学计算器转换  
            //{  
            //    int test = (int)m.WParam;  
            //}  
            base.WndProc(ref m);
        }

        /// <summary>
        /// 设定窗体位置
        /// </summary>
        private void LocatePosition()
        {
            this.WindowState = FormWindowState.Normal;
            this.Width = 180;
            this.Height = 30;
            Rectangle r = Screen.GetWorkingArea(this);
            this.Location = new Point(r.Right - this.Width, r.Bottom - this.Height); //窗体位于右下角
           // this.TopMost = true;

        }

        private void Form1_Resize(object sender, EventArgs e)
        {

        }  

    }

    
       
}
